package com.gcit.seniorWish.web.modular.enroll.service.impl;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.gcit.seniorWish.core.exception.GunsException;
import com.gcit.seniorWish.core.node.SelectNode;
import com.gcit.seniorWish.web.common.constant.cache.Cache;
import com.gcit.seniorWish.web.common.constant.cache.CacheKey;
import com.gcit.seniorWish.web.common.constant.enums.ExamineeStatusEnum;
import com.gcit.seniorWish.web.common.constant.enums.SeniorWishStatusEnum;
import com.gcit.seniorWish.web.common.persistence.dao.SeniorWishMapper;
import com.gcit.seniorWish.web.common.persistence.model.EnrollSchoolBatchMajor;
import com.gcit.seniorWish.web.common.persistence.dao.EnrollSchoolBatchMajorMapper;
import com.gcit.seniorWish.web.common.persistence.model.SeniorWish;
import com.gcit.seniorWish.web.core.exception.BizExceptionEnum;
import com.gcit.seniorWish.web.modular.enroll.dao.EnrollSchoolBatchMajorDao;
import com.gcit.seniorWish.web.modular.enroll.service.IEnrollSchoolBatchMajorService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gcit.seniorWish.web.modular.examinee.dao.SeniorWishDao;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报考专业表 服务实现类
 * </p>
 *
 * @author gaozhichang
 * @since 2018-03-19
 */
@Service
public class EnrollSchoolBatchMajorServiceImpl extends ServiceImpl<EnrollSchoolBatchMajorMapper, EnrollSchoolBatchMajor> implements IEnrollSchoolBatchMajorService {


	@Resource
	private EnrollSchoolBatchMajorDao enrollSchoolBatchMajorDao;

	@Resource
	private SeniorWishDao seniorWishDao;

	/**
	 * 新增报考专业
	 *
	 * @param enrollMajor 新增的专业
	 * @author gaozhichang
	 * @date 2018-03-19 下午 2:53
	 */
	@Override
	@Transactional
	public void insertEnrollMajor(EnrollSchoolBatchMajor enrollMajor) {
		if (baseMapper.selectCount(Condition.create().eq("enroll_school_id", enrollMajor.getEnrollSchoolId())
				.eq("major_code", enrollMajor.getMajorCode())) > 0) {
			throw new GunsException(BizExceptionEnum.ENROLL_MAJOR_CODE_EXISTED);
		}
		baseMapper.insert(enrollMajor);
	}

	/**
	 * 更新报考专业
	 *
	 * @param enrollMajor 修改的专业
	 * @author gaozhichang
	 * @date 2018-03-19 下午 2:59
	 */
	@Override
	@Transactional
	@CacheEvict(value = Cache.BUSS, key = "'" + CacheKey.ENROLL_MAJOR + "'+#enrollMajor.id")
	public void updateEnrollMajor(EnrollSchoolBatchMajor enrollMajor) {
		if (baseMapper.selectCount(Condition.create().ne("id", enrollMajor.getId())
				.eq("enroll_school_id", enrollMajor.getEnrollSchoolId())
				.eq("major_code", enrollMajor.getMajorCode())) > 0) {
			throw new GunsException(BizExceptionEnum.ENROLL_MAJOR_CODE_EXISTED);
		}
		baseMapper.updateById(enrollMajor);
	}

	/**
	 * 查询报考学校列表
	 *
	 * @param page           分页参数
	 * @param enrollSchoolId 查询指定报考学校的专业
	 * @param enrollSchoolId 查询指定的学校
	 * @author gaozhichang
	 * @date 2018-03-20 上午 11:25
	 */
	@Override
	public List listEnrollMajorForSelect2(Page<Map<String, Object>> page, String enrollSchoolId, String majorName, String majorId) {
		//判断当学校id不存在时，不返回专业列表
		if (StrUtil.isBlank(enrollSchoolId)) {
			SelectNode node = new SelectNode();
			node.setId("");
			node.setText("请先选择报考学校");
			node.setDisabled(true);
			return Arrays.asList(node);
		}
		if (StrUtil.isNotBlank(majorName)) {
			majorName = "%" + majorName + "%";
		}
		return enrollSchoolBatchMajorDao.listEnrollMajorForSelect2(page, enrollSchoolId, majorName, majorId);
	}

	/**
	 * 删除专业
	 *
	 * @param majorId 专业id
	 * @author gaozhichang
	 * @date 2018-03-30 下午 3:36login
	 */
	@Override
	@Transactional
	@CacheEvict(value = Cache.BUSS, key = "'" + CacheKey.ENROLL_MAJOR + "'+#majorId")
	public void deleteMajor(String majorId) {
		List<String> majorIdFieldList = new ArrayList<>();
		Field[] fields = SeniorWish.class.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			//设置可以访问私有变量
			field.setAccessible(true);
			if (ReUtil.findAll("Major\\dId", field.getName(), 0).size() > 0) {
				majorIdFieldList.add(StrUtil.toUnderlineCase(field.getName()));
			}
		}
		if(seniorWishDao.selectMajorCheckedCount(majorIdFieldList,majorId)>0){
			throw new GunsException(BizExceptionEnum.ENROLL_MAJOR_ENROLLED);
		}
		baseMapper.deleteById(majorId);
	}

	/**
	 * 查询报考学校列表
	 *
	 * @param page           分页参数
	 * @param enrollSchoolId 查询指定报考学校的专业
	 * @param batchNum 查询指定的学校 指定批次的专业
	 * @author gaozhichang
	 * @date 2018-03-20 上午 11:25
	 */
	@Override
	public List listEnrollMajorForSelect22(Page<Map<String, Object>> page, String enrollSchoolId, int batchNum ) {
		//判断当学校id不存在时，不返回专业列表
		if (StrUtil.isBlank(enrollSchoolId)) {
			SelectNode node = new SelectNode();
			node.setId("");
			node.setText("请先选择报考学校");
			node.setDisabled(true);
			return Arrays.asList(node);
		}

		return enrollSchoolBatchMajorDao.listEnrollMajorForSelect22(page, enrollSchoolId, batchNum);
	}
}
